package com.ruiysoft.service.aftersaleconf.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.entity.aftersaleconf.AftersaleConf;
import com.ruiysoft.mapper.aftersaleconf.AftersaleConfMapper;
import com.ruiysoft.service.aftersaleconf.IAftersaleConfService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liyahui
 * @since 2018-11-24
 */
@Service
public class AftersaleConfServiceImpl extends ServiceImpl<AftersaleConfMapper, AftersaleConf> implements IAftersaleConfService {
    @Override
    @Transactional
    public void setDefByid(Long id, Long shopId) {
        //先把店铺其他模板设置为非默认
        AftersaleConf aftersaleConf = new AftersaleConf().setDefault(false);
        update(aftersaleConf, new QueryWrapper<AftersaleConf>().eq("shop_id", shopId));
        //在设置默认
        updateById(new AftersaleConf().setDefault(true).setId(id));
    }

    @Override
    public void saveAftersaleConf(AftersaleConf aftersaleConf, Long shopId) {
        if (aftersaleConf.getDefault() != null && aftersaleConf.getDefault()) {
            saveOrUpdate(aftersaleConf);
            setDefByid(aftersaleConf.getId(), shopId);
        } else {
            int count = count(new QueryWrapper<AftersaleConf>().eq("shop_id", shopId));
            saveOrUpdate(aftersaleConf);
            if (count <= 0) {
                aftersaleConf.setDefault(true);
                setDefByid(aftersaleConf.getId(), shopId);
            }
        }
    }

}
